const app = getApp()
import http from "../../utils/request.js";
const publicFun = require('../../utils/public');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        collectList: [],
        tabNav: ['收藏', '点赞'],
        TabCur: 0,
        loading: false,
        leftData: [],
        rightData: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.id) {
            this.setData({
                TabCur: options.id
            })
            if (options.id == 0) {
                this.getDreamColloct()
            } else if (options.id == 1) {
                this.getDreamLover()
            }
        } else {
            this.getDreamColloct()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    tabSelect(e) {
        let currIndex = e.detail.index
        this.setData({
            TabCur: currIndex
        })
        if (currIndex == 0) {
            this.getDreamColloct()
        } else if (currIndex == 1) {
            this.getDreamLover()
        }
    },

    //跳转到梦境详情
    toDetail(e) {
        console.log(e.currentTarget.dataset.id);
        wx.navigateTo({
            url: '/page/dreamDetail/dreamDetail?id=' + e.currentTarget.dataset.id,
        })
    },

    //查询收藏梦境
    getDreamColloct() {
        this.setData({
            loading: true,
            collectList: [],
            rightData: [],
            leftData: [],
        })
        let data = {
            userId: app.globalData.userId
        }
        http.Request({
            url: 'myCollect',
            method: 'POST',
            data: data
        }).then(res => {
            if (res.code == 200) {
                let leftData = []
                let rightData = []
                res.data.forEach((item, index) => {
                    if (index % 2) {
                        rightData.push(item)
                    }else{
                        leftData.push(item)
                    }
                })
                this.setData({
                    collectList: res.data,
                    leftData: leftData,
                    rightData: rightData,
                    loading: false,
                });
            } else {
                publicFun.Toast('服务器错误')
            }
        });
    },

    //查询点赞
    getDreamLover() {
        this.setData({
            loading: true,
            collectList: [],
            rightData: [],
            leftData: [],
        })
        let data = {
            userId: app.globalData.userId
        }
        http.Request({
            url: 'myLove',
            method: 'POST',
            data: data
        }).then(res => {
            if (res.code == 200) {
                let leftData = []
                let rightData = []
                res.data.forEach((item, index) => {
                    if (index % 2) {
                        rightData.push(item)
                    }else{
                        leftData.push(item)
                    }
                })
                this.setData({
                    collectList: res.data,
                    rightData: rightData,
                    leftData: leftData,
                    loading: false,
                });
            } else {
                publicFun.Toast('服务器错误')
            }
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})